<script lang="ts" setup>
import type { DataType } from './types';

import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { Page } from '@vben/common-ui';

import { Button, message, Modal } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteUserOauthUsers, getUserOauthUsers } from '#/api/user/oauthUsers';
import Icon from '#/components/Icons';
import { formatImagesList } from '#/utils';

defineOptions({
  name: 'UserAdminOauth',
});

const router = useRouter();
const routeMeta = router.currentRoute.value.meta;
const viewTitle = routeMeta.title.replace('管理', '');

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: false,
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入用户ID',
      },
      fieldName: 'user_id',
      label: '用户ID',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入用户名/昵称/邮箱/手机',
      },
      fieldName: 'keyword',
      label: '关键词',
    },
  ],
  // 控制表单是否显示折叠按钮
  showCollapseButton: false,
  // 按下回车时是否提交表单
  submitOnEnter: false,
};

const gridOptions: VxeGridProps<DataType> = {
  rowConfig: {
    isHover: true,
  },
  columns: [
    { field: 'third_party', title: '来源' },
    { field: 'avatar', title: '头像', cellRender: { name: 'CellAvatar' } },
    { field: 'user', title: '绑定账号', cellRender: { name: 'CellUser' } },
    {
      field: 'create_time',
      title: '首次登录时间',
      formatter: 'formatShortDateTime',
    },
    {
      field: 'last_login_time',
      title: '最后登录时间',
      formatter: 'formatShortDateTime',
    },
    { field: 'last_login_ip', title: '最后登录IP' },
    { field: 'login_times', title: '登录次数' },
    {
      slots: { default: 'action' },
      field: 'action',
      fixed: 'right',
      title: '操作',
      width: 160,
    },
  ],
  height: 'auto',
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        const { data } = await getUserOauthUsers({
          page: page.currentPage,
          limit: page.pageSize,
          ...formValues,
        });
        data.list = formatImagesList(data.list, 'avatar');
        return data;
      },
    },
  },
  toolbarConfig: {
    custom: true,
    // export: true,
    // import: true,
    refresh: true,
    zoom: true,
  },
};

const [Grid, GridApi] = useVbenVxeGrid({
  gridOptions,
  formOptions,
});

const handleDelete = async (row: DataType) => {
  Modal.confirm({
    title: '系统提示',
    content: `确认要删除吗?`,
    async onOk() {
      const $table = GridApi.grid;
      if ($table && row?.id) {
        const { msg } = await deleteUserOauthUsers(row.id);
        $table.remove(row);
        message.success(msg);
      }
    },
  });
};
</script>

<template>
  <Page auto-content-height>
    <Grid :table-title="`${viewTitle}列表`">
      <template #action="{ row }">
        <Button danger type="primary" @click="handleDelete(row)">
          <template #icon>
            <span class="anticon">
              <Icon icon="ri:delete-bin-line" />
            </span>
          </template>
          删除
        </Button>
      </template>
    </Grid>
  </Page>
</template>
